<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\mill\model;

use think\Model;
use think\helper\Hash;
use think\Db;

/**
 * 统计模型
 */
class Stat extends Model
{

    //小程序首页统计同比计算
    public static function doSame($num = 0,$upmum = 0){

        $re = empty($num)?-100:round(($num-$upmum)*100/$num,2);
        if(empty($num) && empty($upmum))$re = 0;
        return $re;

    }

    // 工程阶段统计
    public static function statOrderStatus($starttime = 0,$endtime = 0,$time_input = '')
    {
        //默认今天
        if(empty($starttime))$starttime = strtotime(date("Y-m-d", (time())));
        if(empty($endtime))$endtime = strtotime(date("Y-m-d 23:59:59", time()));

        if(!empty($time_input)){
            $time_input = explode(' - ',$time_input);
            $starttime = strtotime($time_input[0]);
            $endtime = strtotime(date("Y-m-d 23:59:59", strtotime($time_input[1])));
        }

        $date = date('Y-m-d', $starttime);
        $end_date = date('Y-m-d 23:59:59', $endtime);
        //设计
        $bill_where[] = ['o.archives_bill_time','between time',[$date,$end_date]];
        $bill = Db::name('mill_order')
            ->alias('o')
            ->field('sum((a.one_area*a.number)) as area_sum,sum(a.number) as number_sum')
            ->leftJoin('mill_order_archives a','o.id=a.order_id')
            ->where($bill_where)
            ->find();

        //开料
        $status1_where[] = ['g.status','=',1];
        $status1_where[] = ['g.status1_time','between time',[$date,$end_date]];
        $status1 = Db::name('mill_order_archives_give')
            ->alias('g')
            ->field('sum(a.one_area) as area_sum,count(g.id) as number_sum')
            ->leftJoin('mill_order_archives a','a.id=g.archives_id')
            ->where($status1_where)
            ->find();

        //钣金
        $status2_where[] = ['g.status','=',2];
        $status2_where[] = ['g.status2_time','between time',[$date,$end_date]];
        $status2 = Db::name('mill_order_archives_give')
            ->alias('g')
            ->field('sum(a.one_area) as area_sum,count(g.id) as number_sum')
            ->leftJoin('mill_order_archives a','a.id=g.archives_id')
            ->where($status2_where)
            ->find();

        //喷涂
        $status3_where[] = ['g.status','=',3];
        $status3_where[] = ['g.status3_time','between time',[$date,$end_date]];
        $status3 = Db::name('mill_order_archives_give')
            ->alias('g')
            ->field('sum(a.one_area) as area_sum,count(g.id) as number_sum')
            ->leftJoin('mill_order_archives a','a.id=g.archives_id')
            ->where($status3_where)
            ->find();

        //发货
        $status4_where[] = ['g.status','=',4];
        $status4_where[] = ['g.status4_time','between time',[$date,$end_date]];
        $status4 = Db::name('mill_order_archives_give')
            ->alias('g')
            ->field('sum(a.one_area) as area_sum,count(g.id) as number_sum')
            ->leftJoin('mill_order_archives a','a.id=g.archives_id')
            ->where($status4_where)
            ->find();

        $bill['number_sum'] = floatval($bill['number_sum']);
        $status1['number_sum'] = floatval($status1['number_sum']);
        $status2['number_sum'] = floatval($status2['number_sum']);
        $status3['number_sum'] = floatval($status3['number_sum']);
        $status4['number_sum'] = floatval($status4['number_sum']);

        $bill['area_sum'] = floatval($bill['area_sum']);
        $status1['area_sum'] = floatval($status1['area_sum']);
        $status2['area_sum'] = floatval($status2['area_sum']);
        $status3['area_sum'] = floatval($status3['area_sum']);
        $status4['area_sum'] = floatval($status4['area_sum']);

        $re = [
            'bill'          =>  $bill,
            'status1'       =>  $status1,
            'status2'       =>  $status2,
            'status3'       =>  $status3,
            'status4'       =>  $status4
        ];

        return $re;
    }

}
